using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Microsoft.Devices.Sensors;
using Spinning.Spinning.Level;
using Microsoft.Xna.Framework.Input.Touch;

namespace Spinning.Spinning.Entity
{
   abstract class PracticeScene : BaseScene
    {
        public World World { get; protected set; }
        ActorContactManager contactManager;
        public PhysicConnectManager PhysicConnectManager { get; protected set; }
        Sprite failSprite;
        ButtonSprite backSprite;
        public PracticePlayer Player { get; protected set; }
        protected bool isPause;
        protected TimeCountBar timeCountBar;

        public PracticeScene()
            : base()
        {
            PhysicConnectManager = new PhysicConnectManager();
            contactManager = new ActorContactManager(PhysicConnectManager);
            isPause = false;
            Vector2 gravity;
            gravity = new Vector2(0f, 0f);
            bool doSleep = false;
            World = new World(gravity, doSleep);
            World.ContactListener = contactManager;

            creatWall(World, new Vector2(Constants.Window_Width, 2), new Vector2(Constants.Window_Width/2, 0));
            creatWall(World, new Vector2(Constants.Window_Width, 2), new Vector2(Constants.Window_Width/2, Constants.Window_Height));
            creatWall(World, new Vector2(2, Constants.Window_Height), new Vector2(0, Constants.Window_Height/2));
            creatWall(World, new Vector2(2, Constants.Window_Height), new Vector2(Constants.Window_Width, Constants.Window_Height/2));
        }

        public override void OnLoad()
        {
            base.OnLoad();

            Player = new PracticePlayer();
            Player.textureName = "spinning";
            Player.OnLoad();
            Player.SetAnimation(Constants.playerAnimationFrame);
            Player.OnCreatBody(World, PhysicConnectManager);
            Player.BodyPosiotion = new Vector2(240, 600);
            this.Attach(Player);

            failSprite = new Sprite();
            failSprite.textureName = "fail";
            failSprite.ZIndex = Constants.fail;
            failSprite.OnLoad();
            failSprite.Alpha = 0;
            this.Attach(failSprite);

            backSprite = new ButtonSprite();
            backSprite.textureName = "p2/back";
            backSprite.ZIndex = Constants.ui;
            backSprite.Position = new Vector2(240-37, 330);
            backSprite.OnLoad();
            backSprite.Alpha = 0;
            TouchManager.RegisteTouchArea(backSprite);
            this.Attach(backSprite);

            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            SortChildren();
        }

        //protected override void UpdateProjection(GameTime gameTime)
        //{
        //    base.UpdateProjection(gameTime);
        //    Projection = Matrix.Identity;
        //}

        protected override void updateChildren(GameTime gameTime)
        {
            if (!isPause)
            {
                base.updateChildren(gameTime);
            }
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
             if (!isPause)
             {
                 World.Step(1f / 60, 10, 10);
             }
             PhysicConnectManager.OnUpdate(gameTime);
        }

        private void creatWall(World world, Vector2 size, Vector2 poistion)
        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Static;
            Body body = world.CreateBody(bd);
            FixtureDef fd = new FixtureDef();
            PolygonShape ps = new PolygonShape();
            ps.SetAsBox(size.X / Constants.MeterToPixal, size.Y / Constants.MeterToPixal);
            fd.shape = ps;
            Fixture f = body.CreateFixture(fd);
            Vector2 pos = Functional.Vector2InWorld(poistion);
            body.SetTransform(pos, 0);
            Filter filter = new Filter();
            filter.categoryBits = (ushort)Constants.CategoryBitsWall;
            filter.maskBits = (ushort)Constants.MaskBitsWall;
            f.SetFilterData(ref filter);
        }

        protected void DoFail()
        {
            failSprite.Alpha = 1;
            backSprite.Alpha = 1;
            backSprite.OnTouchEvent += OnBackTouch;
            isPause = true;
        }

        void OnBackTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("menu");
        }
    }
}
